package com.goods.controller.business.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import com.goods.controller.business.converter.ProductConverter;
import com.goods.controller.business.service.ProductService;
import com.goods.controller.business.mapper.ProductMapper;
import com.goods.common.vo.business.ProductVO;
import org.apache.commons.collections.Buffer;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired(required=false)
    ProductMapper productMapper;

    /**
     * 物资资料列表
     * @param pageNum
     * @param pageSize
     * @param productVO
     * @return
     */
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {

        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Product.class);
        Example.Criteria criteria = o.createCriteria();
        List<Product> products;

        if(productVO.getStatus() != null){
            criteria.andEqualTo("status",productVO.getStatus());
        }
        if(productVO.getThreeCategoryId() != null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId",productVO.getTwoCategoryId())
                    .andEqualTo("threeCategoryId",productVO.getThreeCategoryId());

            products = productMapper.selectByExample(o);
            List<ProductVO> productVOList = ProductConverter.converterToVOList(products);
            PageInfo<Product> Info = new PageInfo<>(products);
            return new PageVO<>(Info.getTotal(),productVOList);
        }
        if(productVO.getTwoCategoryId() != null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId",productVO.getTwoCategoryId());

            products = productMapper.selectByExample(o);
            List<ProductVO> productVOList = ProductConverter.converterToVOList(products);
            PageInfo<Product> Info = new PageInfo<>(products);
            return new PageVO<>(Info.getTotal(),productVOList);
        }
        if(productVO.getOneCategoryId() != null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId());

            products = productMapper.selectByExample(o);
            List<ProductVO> productVOList = ProductConverter.converterToVOList(products);
            PageInfo<Product> Info = new PageInfo<>(products);
            return new PageVO<>(Info.getTotal(),productVOList);
        }

        o.setOrderByClause("sort asc");
        if(productVO.getName() != null && !"".equals(productVO.getName())){
            criteria.andLike("name","%"+productVO.getName()+"%");
        }

        products = productMapper.selectByExample(o);
        List<ProductVO> productVOList = ProductConverter.converterToVOList(products);
        PageInfo<Product> Info = new PageInfo<>(products);
        return new PageVO<>(Info.getTotal(),productVOList);

        /*String name = productVO.getName();
        Integer status = productVO.getStatus();
        Long[] categoryKeys = productVO.getCategoryKeys();

        if (name != null && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (status != null) {
            criteria.andLike("status", "%" + status + "%");
        }
        if (categoryKeys != null && categoryKeys.length == 3) {
            criteria.andLike("threeCategoryId", "%" + categoryKeys[2] + "%");
        }

        //查询
        List<Product> productList = productMapper.selectByExample(o);
        ArrayList<ProductVO> productVoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(productList)){
            for (Product product : productList) {
                ProductVO productVo = ProductConverter.converterProductVO(product);
                productVoList.add(productVo);
            }
        }
        //转化为分页对象显示
        PageInfo<Product> info=new PageInfo<>(productList);
        return new PageVO<>(info.getTotal(),productVoList);
*/
    }

    /**
     * 添加商品
     * @param productVO
     */
    @Override
    public void add(ProductVO productVO) {

        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());

        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        if(categoryKeys.length == 3){
            product.setOneCategoryId(categoryKeys[0]);
            product.setTwoCategoryId(categoryKeys[1]);
            product.setThreeCategoryId(categoryKeys[2]);
        }
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString().substring(0,32));
        productMapper.insert(product);
    }

    /**
     * 编辑
     * @param id
     * @return
     */
    @Override
    public ProductVO edit(Long id) {

        Product product = productMapper.selectByPrimaryKey(id);

        return ProductConverter.converterToProductVO(product);
    }

    /**
     *修改
     * @param id
     * @param productVO
     */
    @Override
    public void update(Long id, ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());

        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        if(categoryKeys.length == 3){
            product.setOneCategoryId(categoryKeys[0]);
            product.setTwoCategoryId(categoryKeys[1]);
            product.setThreeCategoryId(categoryKeys[2]);
        }
        productMapper.updateByPrimaryKey(product);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) throws Exception{

        Product product = new Product();
        product.setId(id);

        Product p = productMapper.selectByPrimaryKey(product);
        if(p.getStatus() != 1 && p.getStatus() != 2){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }else {
            productMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 移入回收站
     * @param id
     */
    @Override
    public void remove(Long id) throws Exception{

        Product p = productMapper.selectByPrimaryKey(id);
        if(p.getStatus() != 0){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }else {
            p.setStatus(1);
            productMapper.updateByPrimaryKey(p);
        }
    }

    /**
     * 回收站恢复
     * @param id
     */
    @Override
    public void back(Long id) throws BusinessException {
        Product product = productMapper.selectByPrimaryKey(id);
        if(product.getStatus() != 1){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }else {
            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
        }
    }
    /**
     * 审核
     * @param id
     */
    @Override
    public void publish(Long id) throws BusinessException {

        Product product = productMapper.selectByPrimaryKey(id);
        if(product.getStatus() != 2){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }else {
            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
        }
    }


}
